# TCP server example
import socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("", 6691))
server_socket.listen(0)

print "TCPServer Waiting for client on port 6690"

while 1:
	client_socket, address = server_socket.accept()
	print "(II) Got a connection from ", address
	socket_closed = False
	while not socket_closed:
		data = client_socket.recv(512)
		print "(II) RECIEVED:" , data
		
		# 1= GET_DISPLAYS
		if data[0:1] == '1':
			print "(II) Sent reply"
			client_socket.send ("LEN|CRT-1\n")
			
		# 2 = SET_DISPLAYS
		elif data[0:1] == '2':
			print "(II) Request for setting up screens: " + data[1:]

		# 3 = CLOSE_CONNECTION
		elif data[0:1] == '3':
			client_socket.close
			print "(II) Closing socket"
			socket_closed=True

